/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.person;

import enteties.person.address.Address;
import enteties.person.address.AddressFacadeLocal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.person.SupplierDetails;

/**
 *
 * @author Gebruiker
 */
@Stateless
public class SupplierFacade extends AbstractFacade<Supplier> implements SupplierFacadeLocal {
    @EJB
    private AddressFacadeLocal addressFacade;
    @EJB
    private LanguageFacadeLocal languageFacade;
    @EJB
    private TelecomFacadeLocal telecomFacade;
    
    
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public SupplierFacade() {
        super(Supplier.class);
    }

    @Override
    public Integer createSupplier(String name, Integer number, Collection<Long> telecom, Integer languageid, Long addressid) {
        Collection<Telecom> telecomCollection = new ArrayList<Telecom>();
//        for (Long telecomId : telecom) {
//            telecomCollection.add(telecomFacade.find(telecomId));
//        }
        Language language = languageFacade.find(languageid);
        Address address = addressFacade.find(addressid);
        Supplier supplier = new Supplier(name, number, telecomCollection, language, address);
        create(supplier);
        return supplier.getId();
    }
    
    @Override
    public void editSupplier(Integer id, String name, Integer number, Collection<Long> telecom, Integer languageid, Long addressid){
        Supplier supplier = find(id);
        supplier.setName(name);
        supplier.setNumber(number);
        supplier.setLanguageid(languageFacade.find(languageid));
        supplier.setAddressid(addressFacade.find(addressid));
        edit(supplier);
    }
    
    @Override
    public void removeSupplier(Integer id){
        Supplier supplier = find(id);
        remove(supplier);
    }
    
    @Override
    public boolean exists(Integer number){
        return findByNumber(number) != null;
    }
    
    @Override
    public SupplierDetails getSupplierDetails(Integer id){
        return convert(find(id));
    }

    @Override
    public SupplierDetails findByNumber(Integer number) {
        List<Supplier> suppliers = (List<Supplier>) em.createNamedQuery("Supplier.findByNumber").setParameter("number", number).getResultList();
        if (suppliers.size() == 1) {
            return convert(suppliers.get(0));
        } else if (suppliers.size() > 1) {
            throw new IllegalStateException();
        } else {
            return null;
        }
    }
    
    @Override
    public Collection<SupplierDetails> findAllSuppliers(){
        Collection<SupplierDetails> suppliers = new ArrayList<SupplierDetails>();
        for(Supplier supplier:findAll()){
            suppliers.add(convert(supplier));
        }
        return suppliers;
    }
    
    @Override
    public void addTelecomToSupplier(Integer supplierId, Integer telecomId){
        Supplier supplier = find(supplierId);
        supplier.addTelecom(telecomFacade.find(telecomId));
        edit(supplier);
    }
    
    @Override
    public SupplierDetails convert (Supplier supplier){
        Collection<Integer> telecomCollection = new ArrayList<Integer>();
        for(Telecom tele:supplier.getTelecomCollection()){
            telecomCollection.add(tele.getId());
        }
        Integer languageId = supplier.getLanguageid().getId();
        Long addressId = supplier.getAddressid().getId();
        return new SupplierDetails(supplier.getId(), supplier.getName(), supplier.getNumber(), telecomCollection, languageId, addressId);
        
    }
}
